package procsim;

public class Shifter extends Register{
    private Signal shlS, shrS;
    public Shifter(int numOfBits, int init, MSignal in){
        super(numOfBits, init, in);
        name="SH";
    }
    public Shifter(int numOfBits, int init, MSignal in, String name){
        super(numOfBits, init, in, name);
    }
    public Shifter shl(){
        val=(val<<1)%(int)Math.pow(2, bits);
        return this;
    }
    public Shifter shr(){
        val=(val>>1)%(int)Math.pow(2, bits);
        return this;
    }
    public Shifter setShiftLeftSignal(Signal shl){
        if(shl!=null) shl.addElement(this);
        shlS=shl;
        if(shrS!=null) shrS.set(0);
        deactivateLoadClear();
        return this;
    }
    public Shifter setShiftRightSignal(Signal shr){
        if(shr!=null) shr.addElement(this);
        shrS=shr;
        if(shlS!=null) shlS.set(0);
        deactivateLoadClear();
        return this;
    }
    @Override
    public Shifter tick() {
        if(shlS!=null && !shlS.isNull())
            shl();
        if(shlS!=null && !shrS.isNull())
            shr();
        super.tick();
        return this;
    }
    @Override
    public void updateSignal(Sig sig){
        super.updateSignal(sig);
        if(shlS!=null && shlS==sig)
            shlS.set(sig.get(), false);
        if(shrS!=null && shrS==sig)
            shrS.set(sig.get(), false);
    }
}
